﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using GCT.Foundation.Configuration;
using GCT.Foundation.Helpers;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

namespace GCT.Foundation.EntityFrameworkCore
{
    public class FoundationTemplateDbContextFactory : IDesignTimeDbContextFactory<FoundationTemplateDbContext>
    {
        public FoundationTemplateDbContext CreateDbContext(string[] args)
        {
            var builder = new DbContextOptionsBuilder<FoundationTemplateDbContext>();

            var basePath = FoundationTemplateWebContentDirectoryFinder.CalculateContentRootFolder();
            System.Console.WriteLine("工作目录：");
            System.Console.WriteLine(basePath);

            var configuration = AppConfigurations.Get(basePath, "Development");

            var connectionString = configuration.ConnectionStringsDefault();

            System.Console.WriteLine("迁移使用数据库连接字符串：");
            System.Console.WriteLine(connectionString);

            FoundationTemplateDbContextConfigurer.Configure(builder, connectionString);

            return new FoundationTemplateDbContext(builder.Options);
        }
    }
}
